package com.zhijiang.hospital.controller;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zhijiang.common.util.ApiResult;
import com.zhijiang.hospital.entrty.*;
import com.zhijiang.hospital.entrty.dto.EmployeeDto;
import com.zhijiang.hospital.entrty.dto.RepairDto;
import com.zhijiang.hospital.mapper.EmployeeDtoMapper;
import com.zhijiang.hospital.mapper.EmpolyeeMapper;
import com.zhijiang.hospital.service.RepairService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("repair")
public class RepairController {

    @Autowired
    RepairService repairService;
    //维修申请
    @PostMapping("repairApplication")
    public ApiResult repairApplication(@RequestBody Repair repair){
        System.out.println(repair);
           String l=repairService.repairApplication(repair);
        return ApiResult.ok(l);
    }

    @GetMapping("employee")
    public ApiResult getEmployee(String emplCode){

        EmployeeDto employee = repairService.getEmployee(emplCode);
        return ApiResult.ok(employee);
    }
    @Resource
    EmployeeDtoMapper employeeDtoMapper;
    @Resource
    EmpolyeeMapper empolyeeMapper;
    //维修查询
    @GetMapping("selectAllRepairOrder")
    public ApiResult selectAllRepairOrder(String startTime, String endTime){

        List<Repair> repairs = repairService.selectAll(startTime, endTime);
        repairs.stream().forEach(s->{
            System.out.println(s.getRepairApplyOperator());
            if (StringUtils.isBlank(s.getRepairApplyPhone())){
            }else {
                EmployeeDto employeesByPhone = employeeDtoMapper.findEmployeesByPhone(s.getRepairApplyPhone());
               if (Objects.isNull(employeesByPhone)){
                }else{

                    s.setRepairApplyDept(employeesByPhone.getDeptName());
                    s.setRepairApplyName(employeesByPhone.getEmplName());
                }
            }

            if (StringUtils.isBlank(s.getRepairOperator())){

            }else {
                EmployeeDto oneEmployeeDto = employeeDtoMapper.findOneEmployeeDto(s.getRepairOperator());
               if (Objects.isNull(oneEmployeeDto)){

               }else {
                   s.setRepairOperator(oneEmployeeDto.getEmplName());
               }


            }

        });
        return ApiResult.ok(repairs);
    }




    //维修详细
    @GetMapping("SelectRepair")
    public ApiResult SelectRepair(String  repairApplyId ){
        //查询订单
          Repair repair  = repairService.selectByRepairApllyid(repairApplyId);
          //查询图片
          List<TImage> tImages = repairService.selectImageByRepairApllyid(repairApplyId);
        List<Progress> progress   = repairService.selectProgressAllByRepairId(repairApplyId);
        RepairDto repairDto = new RepairDto();
        BeanUtils.copyProperties(repair,repairDto);
        repairDto.setProgresss(progress);
        repairDto.setTImages(tImages);
        return ApiResult.ok(repairDto);
    }



    //人员导入模板
    @PostMapping("templateExcel")
    public void templateExcel(HttpServletResponse response,String startTime, String endTime) throws IOException {
        List<Repair> repairs = repairService.selectAll(startTime, endTime);
        repairs.stream().forEach(s->{
            System.out.println(s.getRepairApplyOperator());
            if (StringUtils.isBlank(s.getRepairApplyPhone())){
            }else {
                EmployeeDto employeesByPhone = employeeDtoMapper.findEmployeesByPhone(s.getRepairApplyPhone());
                if (Objects.isNull(employeesByPhone)){
                }else{
                    System.out.println(employeesByPhone);
                    s.setRepairApplyDept(employeesByPhone.getDeptName());
                    s.setRepairApplyName(employeesByPhone.getEmplName());
                }
            }

            if (StringUtils.isBlank(s.getRepairOperator())){

            }else {
                EmployeeDto oneEmployeeDto = employeeDtoMapper.findOneEmployeeDto(s.getRepairOperator());
                if (Objects.isNull(oneEmployeeDto)){

                }else {
                    s.setRepairOperator(oneEmployeeDto.getEmplName());
                }


            }

        });
        String title = "维修记录模板";
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + RandomUtil.randomNumbers(4) + URLEncoder.encode("院外人员导入模板.xlsx","utf-8"));
        //4. 创建输入、输出流
        EasyExcel.write(response.getOutputStream(), Repair.class).sheet(title).doWrite(repairs);

    }

    //    //员工搜索接口
    @GetMapping("getList")
    public ApiResult getList(){
        List<String> names =   repairService.getList();
        return  ApiResult.ok(names);
    }

    //获取员工
    @GetMapping(value = "/selectDutyTable1")
    public ApiResult selectDutyTable1(){
        List<DutyTable> dutyTables =  repairService.selectXxkPaiban1();
        HashMap<String, List<DutyTable>> map = new HashMap<>();
        for (DutyTable dutyTable : dutyTables) {
            dutyTable.getZhiBanRi();
            map.put( dutyTable.getZhiBanRi().toString(),dutyTables);

        }

        return  ApiResult.ok(map);
    }


}
